package com.geeguo.ebuilder.operation.config;

import com.geeguo.ebuilder.client.base.security.interceptor.TokenInterceptor;
import com.geeguo.ebuilder.client.base.tenant.interceptor.TenantInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class GlobalWebMvcConfigurer implements WebMvcConfigurer {

    /** 不需要拦截地址 */
    public static final String[] tokenInterceptorExcludeUrls = {
            "/system/authorization/getTenantCode",
            "/system/authorization/getCaptcha",
            "/system/authorization/login",
            "/system/authorization/logout",
            "/v3/api-docs/*"
        };

    /** 不需要拦截地址 */
    public static final String[] tenantInterceptorExcludeUrls = {
            "/system/authorization/getTenantCode",
            "/system/authorization/getCaptcha",
            "/system/authorization/login",
            "/system/authorization/logout",
            "/v3/api-docs/*"
        };

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")      // 添加映射路径，"/**"表示对所有的路径实行全局跨域访问权限的设置
                .allowedOriginPatterns("*")       // 开放哪些IP、端口、域名的访问权限
                .allowCredentials(true)           // 是否允许发送Cookie信息
                .allowedMethods("GET", "POST")    // 开放哪些HTTP方法，允许跨域访问
                .allowedHeaders("*")              // 允许HTTP请求中的携带哪些Header信息
                .exposedHeaders("*");             // 暴露哪些头部信息（因为跨域访问默认不能获取全部头部信息）
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new TokenInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(tokenInterceptorExcludeUrls)
                .order(1);

        registry.addInterceptor(new TenantInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(tenantInterceptorExcludeUrls)
                .order(2);
    }
}